import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.zbcz.crm.R
import com.zbcz.crm.bean.FollowPersonVo

class FollowPersonAdapter :
    ListAdapter<FollowPersonVo, FollowPersonAdapter.ViewHolder>(FollowPersonDiffCallback()) {

    var mDelClick: ((Int, FollowPersonVo) -> Unit?)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_add_followers, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val followNameEditText: EditText = itemView.findViewById(R.id.ed_follower_name)
        private val followIdCardEditText: EditText =
            itemView.findViewById(R.id.ed_tv_follower_id_card)
        private val followMobileNoEditText: EditText =
            itemView.findViewById(R.id.ed_follower_cellphone)
        private val followEmployerEditText: EditText =
            itemView.findViewById(R.id.ed_follower_department)
        private val deleteImageView: ImageView = itemView.findViewById(R.id.iv_del)

        init {
            deleteImageView.setOnClickListener {
//                val position = adapterPosition
//                if (position != RecyclerView.NO_POSITION) {
//                    // Remove the item from the data list
//                    removeItem(position)
//                }
                mDelClick?.invoke(adapterPosition, getItem(adapterPosition))
            }

            followNameEditText.addTextChangedListener {
                getItem(adapterPosition).followName = it.toString()
            }
            followIdCardEditText.addTextChangedListener {
                getItem(adapterPosition).followIdNo = it.toString()
            }
            followMobileNoEditText.addTextChangedListener {
                getItem(adapterPosition).followMobileNo = it.toString()
            }
            followEmployerEditText.addTextChangedListener {
                getItem(adapterPosition).followEmployer = it.toString()
            }
        }

        fun bind(item: FollowPersonVo) {
            followNameEditText.setText(item.followName ?: "")
            followIdCardEditText.setText(item.followIdNo ?: "")
            followMobileNoEditText.setText(item.followMobileNo ?: "")
            followEmployerEditText.setText(item.followEmployer ?: "")
        }

        fun getEditTextValues(): FollowPersonVo {
            return FollowPersonVo(
                followNameEditText.text.toString(),
                followIdCardEditText.text.toString(),
                followMobileNoEditText.text.toString(),
                followEmployerEditText.text.toString()
            )
        }
    }

    fun getAllEditTextValues(): List<FollowPersonVo> {
        val values = mutableListOf<FollowPersonVo>()
        for (i in 0 until itemCount) {
            values.add(getItem(i))
        }
        return values
    }

    fun removeItem(position: Int) {
        if (position != RecyclerView.NO_POSITION) {
            // Remove the item from the data list
            currentList.toMutableList().removeAt(position)
            // Notify adapter about item removal
            notifyItemRemoved(position)
        }
    }
}

class FollowPersonDiffCallback : DiffUtil.ItemCallback<FollowPersonVo>() {
    override fun areItemsTheSame(oldItem: FollowPersonVo, newItem: FollowPersonVo): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: FollowPersonVo, newItem: FollowPersonVo): Boolean {
        return oldItem.followName == newItem.followName &&
                oldItem.followIdNo == newItem.followIdNo &&
                oldItem.followMobileNo == newItem.followMobileNo &&
                oldItem.followEmployer == newItem.followEmployer
    }
}
